package love.yuanmeng.guetcoalsystem.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
//import com.sun.org.apache.bcel.internal.generic.LNEG;
import love.yuanmeng.guetcoalsystem.mapper.EnterpriseInfoMapper;
import love.yuanmeng.guetcoalsystem.mapper.UserMapper;
import love.yuanmeng.guetcoalsystem.mapper.BondMapper;

import love.yuanmeng.guetcoalsystem.pojo.Bond;
import love.yuanmeng.guetcoalsystem.pojo.BondEnterpriseMap;
import love.yuanmeng.guetcoalsystem.pojo.EnterpriseInfo;
import love.yuanmeng.guetcoalsystem.pojo.User;
import love.yuanmeng.guetcoalsystem.service.BondService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class BondServiceImpl implements BondService {
    private static final String SUCCESS = "SUCCESS";
    private static final String ERROR = "ERROR";

    @Resource
    private BondMapper bondmapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private EnterpriseInfoMapper enterpriseInfoMapper;

    @Override
    public String addbond(String id, String price) {
        User user;
        Bond bo2 = null;
        Integer id2 = Integer.parseInt(id);
        user = userMapper.selectById(id2);
        if (user != null) {
            bo2 = bondmapper.selectbo(user.getEnterpriseid());
            double price1 = Double.parseDouble(price);
            if (bo2 != null) {
                String a = bo2.getAccount_balance();
                String b = bo2.getUnfrozen_amount();
                double a2=Double.parseDouble(a);
                double b2=Double.parseDouble(b);
                double a1 = a2 + price1;
                double b1 =  b2+ price1;
                a = String.valueOf(a1);
                b = String.valueOf(b1);
                String bondid = String.valueOf(bo2.getBond_id());
               int result= bondmapper.updatebo(bondid, a, b);
                if (result == 1)
                    return SUCCESS;
                else
                    return ERROR;
            }
            else {
                if(1==bondmapper.addbo(user.getEnterpriseid(),price))
                    return SUCCESS;
                else
                    return ERROR;
            }
        }
        else return ERROR;
    }


    @Override
    public BondEnterpriseMap selectbond(String id){
        User user;
        Bond bo2 = null;
        EnterpriseInfo enterpriseInfo = null;
        Integer id2=Integer.parseInt(id);
        user=userMapper.selectById(id2);
        if(user!=null){
            QueryWrapper<Bond> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("enterprise_id",user.getEnterpriseid());
            bo2=bondmapper.selectbo(user.getEnterpriseid());

            BigDecimal bd1 = new BigDecimal(bo2.getAccount_balance());
            bo2.setAccount_balance(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            bd1 = new BigDecimal(bo2.getUnfrozen_amount());
            bo2.setUnfrozen_amount(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            bd1 = new BigDecimal(bo2.getContract_frozen_amount());
            bo2.setContract_frozen_amount(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());

            enterpriseInfo=enterpriseInfoMapper.selectEnter(user.getEnterpriseid());
        }
        BondEnterpriseMap bondEnterpriseMap=new BondEnterpriseMap(bo2,enterpriseInfo);
        return bondEnterpriseMap;
    }
}
